package com.tbs.parkingsaleapp.Http.Listenner;

import abstergo.tbs.betterasyc.AsycBase.IAsycBackgroundAction;
import abstergo.tbs.betterasyc.AsycBase.IAsycResultAction;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.tbs.parkingsaleapp.Http.HttpHelper;
import com.tbs.parkingsaleapp.Http.JsonData.LoginResult;
import com.tbs.parkingsaleapp.SystemManager.NetWorkSetting;
import com.tbs.parkingsaleapp.SystemManager.SystemDataMananger;
import com.tbs.parkingsaleapp.UI.LoginOrSign.Base.CommonLoginFragment;
import net.steamcrafted.loadtoast.LoadToast;

public class CommonLoginHttper implements IAsycBackgroundAction<String, LoginResult>, IAsycResultAction<LoginResult> {
    CommonLoginFragment context;
    LoadToast toast;
    public CommonLoginHttper(CommonLoginFragment context)
    {
        this.context=context;
        toast=new LoadToast(context.getContext());
        toast.setTranslationY(250);
    }
    public void Show()
    {
        toast.setText("正在登录...");
        toast.show();
    }
    public void Hide()
    {
        toast.hide();
    }
    @Override
    public LoginResult BackGroundAction(String... params) throws Exception{
        String r= HttpHelper.Get( NetWorkSetting.CommonLoginUrl(params[0],params[1]));
        LoginResult result= JSON.parseObject(r,LoginResult.class);
        return result;
    }

    @Override
    public void ResultFunction(LoginResult result) {
        Hide();
        switch (result.getResult())
        {
            case 1:
                SystemDataMananger.Login(result,context.IsRemember(), context.getContext());
                Toast.makeText(context.getContext(),"登录成功",Toast.LENGTH_SHORT).show();
                (context.getActivity()).finish();
                break;
            case 0:
                Toast.makeText(context.getContext(),"密码错误",Toast.LENGTH_SHORT).show();
                break;
            case -1:
                Toast.makeText(context.getContext(),"无效用户名",Toast.LENGTH_SHORT).show();
                break;
        }


    }
}
